package cn.ncupyh;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static java.sql.DriverManager.getConnection;

public class JDBCMain {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.连接数据库
        String uri = "jdbc:mysql://127.0.0.1:3306/21soft?serverTimezone=GMT%2B8&userSSL=false";
        String root = "root";
        String password = "djx020601";
        Connection conn = getConnection(uri, root, password);
        //定义SQL语句
        String sql = "SELECT * from user where uid='1'";
        // 3.创建一个发送SQL语句的对象
        Statement stmt = conn.createStatement();
        // 4.发送SQL语句进行查询 并返回结果
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()){
            int anInt = rs.getInt(1);
            String uname = rs.getString(2);
            String upassword = rs.getString("upassword");
            System.out.println(anInt+"\t"+uname+"\t"+upassword);
        }
        //8.关闭资源
        rs.close();
        stmt.close();
        conn.close();
    }
}
